<?php
/**
 * YOS News Crawler Component
 *
 * @package		yos_news_crawler
 * @subpackage	CMS
 * @link		http://yopensource.com
 * @author		ducdm@f5vietnam.com
 * @copyright 	yopensource (yopensource@gmail.com)
 * @license		Commercial
 */

// no direct access
defined('_JEXEC') or die('Restricted access');


class YOS_Drupal_article extends JObject {
	//object article
	private $_nid;
	
	// dbo
	private $_dbo=null;
	
	public $msg=null;
	
	public function __construct($nid)
	{
		$this->_nid = $nid;
		if(!$this->getDBO())
		{
			return $this->msg;
		}
	}
	function getDBO()
	{
		require(JPATH_COMPONENT_ADMINISTRATOR.DS.'cms'.DS.'drupalpost'.DS.'cms_config.php');		
		$option_ 	= array(); //prevent problems 
		$option_['driver']   = $d_w_driver;       	// Database driver name
		$option_['host']     = $d_w_host;    		// Database host name
		$option_['user']     = $d_w_user;  	    	// User for database authentication
		$option_['password'] = $d_w_password;  		// Password for database authentication
		$option_['database'] = $d_w_database;  	  	// Database name
		$option_['prefix']   = $d_w_prefix;          // Database prefix (may be empty)
		if (empty($option_['user']) || empty($option_['password']) || empty($option_['database']) || empty($option_['driver']) || empty($option_['host']))
		{
			$config	=	JFactory::getConfig();
			
			$option_['driver']   = $config->getValue('dbtype');       	// Database driver name
			$option_['host']     = $config->getValue('host');    		// Database host name
			$option_['user']     = $config->getValue('user');  	    	// User for database authentication
			$option_['password'] = $config->getValue('password');  		// Password for database authentication
			$option_['database'] = $config->getValue('db');  	  	// Database name
		}
		$db_	= &JDatabase::getInstance( $option_ );
		if (!empty($db_->message)) {
				$this->msg = JText::sprintf($db_->message);
				return false;
			}	
		$this->_dbo=$db_;
		return $this->_dbo;
	}
	public function deleteArticle()
	{		
		$db_=$this->_dbo;
		//////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////
		// delete from node
		$query 	= ' DELETE FROM '.$db_->nameQuote('#__node')
						. ' WHERE nid='.$this->_nid;
		$db_->setQuery($query);
		
		if (!$db_->query())
		{
			$this->msg=$db_->getErrorMsg();
			return  false;
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////
		// DELETE FROM NODE_COMMENT_STATISTICS
		$query 	= ' DELETE FROM '.$db_->nameQuote('#__node_comment_statistics')
				. ' WHERE nid='.$this->_nid;				
		$db_->setQuery($query);
		if (!$db_->query())
		{
			$this->msg=$db_->getErrorMsg();
			return false;
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////
		// DELETE FROM NODE_REVISIONS
		$query 	= ' DELETE FROM '.$db_->nameQuote('#__node_revisions')
				. ' WHERE nid='.$this->_nid;				
		$db_->setQuery($query);
		if (!$db_->query())
		{
			$this->msg=$db_->getErrorMsg();
			return false;
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////
		// DELETE FROM TERM_NODE
		$query 	= ' DELETE FROM '.$db_->nameQuote('#__term_node')
				. ' WHERE nid='.$this->_nid;				
		$db_->setQuery($query);
		if (!$db_->query())
		{
			$this->msg=$db_->getErrorMsg();
			return false;
		}
		//////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////
		// DELETE FROM NODE_ACCESS
		$query 	= ' DELETE FROM '.$db_->nameQuote('#__node_access')
				. ' WHERE nid='.$this->_nid;				
		$db_->setQuery($query);
		if (!$db_->query())
		{
			$this->msg=$db_->getErrorMsg();
			return false;
		}
		return true;
	}
}